Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

278
Visualizações
Short and efficient Ruby code for transforming array of words to shuffled and comma separated string, with an "and" before the last word?

I would like to transform such array:

["apple","banana","pineapple","plum","pear"]

to

I like plum, pineapple, banana, pear *and* apple.

My current code is like:

%Q{
   I like #{%w[apple banana pineapple plum pear].shuffle.yield_self{|q| [q[0..-2].join(", "),q[-1]].join(" and ") }}
  }

Is there any more %w[clever clean shorter efficient rubyesque].magic() approach?

over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

I don't know if this is more clever or cleaner, but here we go:

def to_sentence(ary)
  ary = ary.dup.shuffle
  [ary.pop, ary.join(', ')].reverse.join(' and ')
end
over 4 years ago · Santiago Trujillo Relatório

0

I would like to offer two solutions. Neither mutates the array that is passed to the method as an argument.

arr = ["apple", "banana", "pineapple", "plum", "pear"]

#1

 def to_sentence(arr)
  *all_but_last, last = arr.shuffle
  all_but_last.join(', ') << ' and ' << last
end
to_sentence(arr)
  #=> "pear, plum, pineapple, apple and banana"
to_sentence(arr)
  #=> "banana, pear, pineapple, plum and apple"
to_sentence(arr)
  #=> "banana, plum, apple, pineapple and pear"
to_sentence(arr)
  #=> "banana, pineapple, pear, apple and plum"

#2

 def to_sentence(arr)
  arr.shuffle.join(', ').sub(/,(?!.*,)/, ' and')
 end
 to_sentence(arr)
   #=> "banana, plum, pear, apple and pineapple"
 to_sentence(arr)
   #=> "pear, plum, pineapple, banana and apple"
 to_sentence(arr)
   #=> "apple, pineapple, plum, pear and banana"
 to_sentence(arr)
   #=> "apple, plum, pear, pineapple and banana"

The regular expression matches a comma that is not followed later in the string by another comma (that is, it matches the last comma in the string). (?!.*,) is a negative lookahead.

The regular expression could also be written

/.*\K,/

.* matches any character other than line terminators, as many as possible (including commas), because * is by default greedy. \K then resets the start of the match to the current location in the string (right before the last comma) and discards any previously-consumed characters from the match that is returned. A comma is then matched, which necessarily is the last comma in the string.

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda